<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" 	uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" 	%>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" 	%> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<jsp:include page="./dbChatConfig.jsp" flush="false" />

<sql:transaction dataSource="${oracleDs}" >
	<sql:update var="chatRoomDelCnt" >
		/* 채팅방 삭제 */
		UPDATE chat_room 
		SET chat_room_valid = 0 
		WHERE 1 = 1 
		AND chat_room_id 	= ?  
		AND chat_room_valid = 1
		<sql:param value="${ param.delChatRoomId }"/> 
		/* 끝. 채팅방 삭제 */
	</sql:update> 
	<sql:update var="chatRoomInsCnt" >
		/* 채팅방 생성 */
		INSERT INTO chat_room 
			( chat_room_id, chat_room_name , chat_room_user_name ) 
		SELECT 
			  CHAT_ROOM_ID_SEQ.NEXTVAL , ? , ? 
		FROM dual
		WHERE 1 = 1 
		AND ? IS NOT NULL
		AND ? IS NOT NULL 
		<sql:param value="${ param.chatRoomName }"/>
		<sql:param value="${ param.chatRoomUserName }"/> 
		<sql:param value="${ param.chatRoomName }"/>
		<sql:param value="${ param.chatRoomUserName }"/> 
		/* 끝. 채팅방 생성 */
	</sql:update> 
</sql:transaction>

<c:set var="pageRowCnt" value="10" />
<c:set var="viewPageNo" value="${ param.viewPageNo eq null ? '1' : param.viewPageNo }" />
<c:set var="viewPageNo" value="${ viewPageNo + 0 }" />

<sql:query var="chatRoomList" scope="request" 
	dataSource="${oracleDs}" 
	startRow="0" maxRows="100"
>
	/* 채팅방 목록 조회 */
	/* 하루전에 만들어진 것은 조회하지 않는다. */
	SELECT 
	       chat_room.*
	  FROM
	       (SELECT
	       		  CEIL( COUNT( * ) over() / page_row_cnt ) AS max_page_no , 
	       		  COUNT( * ) over() AS rec_cnt ,
	              CEIL( ROWNUM / page_row_cnt ) AS page_no ,
	              ROWNUM AS rno ,
	              chat_room.*,
	              page.*
	         FROM
	              (SELECT NVL( ?,  1 ) AS view_page_no ,
	                      NVL( ?, 10 ) AS page_row_cnt 	                      
	                FROM dual
	              ) page ,
	              (SELECT chat_room_id ,
	                     chat_room_name ,
	                     NVL( chat_room_name, '방제 없음' ) AS chat_room_disp_name ,
	                     chat_room_user_name,
	                     TO_CHAR( chat_room_cre_date,'YYYY-MM-DD HH24:MI:SS') AS chat_room_cre_date ,
	                     1 AS a
	                FROM chat_room
	               WHERE 1 = 1
	                     AND chat_room_valid = 1
	                     AND chat_room_cre_date > SYSTIMESTAMP - 1
	            	ORDER BY chat_room_id
	              ) chat_room
	       ) chat_room
	 WHERE 1 = 1
	 AND page_no  = least( view_page_no, max_page_no ) 
	 <sql:param value="${ viewPageNo }" />
	 <sql:param value="${ pageRowCnt }" />
	/* 끝. 채팅방 목록 조회 */
</sql:query> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> DB 채팅 방 목록 </title>
<link rel="stylesheet" type="text/css" href="../css/chat.css">

<script>
	function delChatRoom() {
		var cf = confirm( '채팅방을 삭제하시겠습니까?' ) ;
		
		if( cf ) {
			var f = document.getElementById( "chatRoomsForm" );
			// 삭제할 때는 현재 페이지로 액션을 설정한다.
			f.action = ""; 
		} else {
			
		}
		
		return cf;
	}
</script>
</head>
<body> 
	<hr/>
	<h3><a href="../index.html">첫 페이지</a></h3> 
	<p> 
		${ chatRoomInsCnt > 0 ? '채팅방이 만들어 졌습니다.' : '&nbsp;' }  <br/>
	 	${ chatRoomDelCnt > 0 ? '채팅방이 삭제되었습니다.' : '&nbsp;' }
	</p>
	<div>
		<hr/> 
		<form id="chatRoomsForm" method="GET" action="dbChatSend.jsp" target="_self" >
			<div class="datagrid" style="width: 700px; text-align: center; " >
				<table >
					<thead>
						<tr class='alt'>
							<th colspan="5" > DB 채팅방 목록 </th>
						</tr>
						<tr>
							<th width="30px">번호</th>
							<th>방제</th>
							<th width="60px">방장</th>
							<th width="120px">개설일시</th>
							<th width="105px">메뉴</th>
						</tr>
					</thead>
					<tbody> 
						<c:set var="recCnt" value="0" />
						<c:set var="maxPageNo" value="0" />
						<c:set var="viewPageNo" value="1" />
						<c:set var="rowCnt" value="${ chatRoomList.rowCount }" />
						<c:set var="lastRowNo" value = "1" />

						<c:forEach items="${ chatRoomList.rows }" var="row" varStatus="s" >
							<c:if test="${s.first}">
								<c:set var="recCnt" 	value="${ row.REC_CNT 		}" />
								<c:set var="maxPageNo" 	value="${ row.MAX_PAGE_NO 	}" />
								<c:set var="viewPageNo" value="${ row.VIEW_PAGE_NO	}" />
							</c:if>
							<c:if test="${s.last}">
								<c:set var="lastRowNo" value = "${ row.RNO }" />
							</c:if>
							<tr class="${ ( s.index % 2 ) == 1 ? 'alt' : '' }" height="34px" >
								<td>
									${ row.RNO }
								</td>
								<td>
									${ row.CHAT_ROOM_DISP_NAME }
								</td>
								<td>
									${ row.CHAT_ROOM_USER_NAME }
								</td>
								<td>
									${ row.CHAT_ROOM_CRE_DATE  }
								</td>
								<td> 
									<button name="chatRoomId" 	 value="${ row.CHAT_ROOM_ID }" 									>입장</button>
									<button name="delChatRoomId" value="${ row.CHAT_ROOM_ID }" onclick="return delChatRoom();" 	>삭제</button>
								</td>							
							</tr> 
						</c:forEach>
						
						<c:forEach var="rNo" begin="${ 1 }" end="${ pageRowCnt - rowCnt }" varStatus="s" >
							<tr class="${ ( s.index % 2 ) == 0 ? 'alt' : '' }" height="34px" >
								<td>
									${ lastRowNo + s.index }
								</td>
								<td>
									&nbsp;
								</td>
								<td>
									&nbsp;
								</td>
								<td>
									&nbsp;
								</td>
								<td> 
									&nbsp;
									&nbsp;
								</td>							
							</tr>
						</c:forEach>
					</tbody>
					<tfoot>
						<tr height="30px">
							<td colspan="5">
								<div>
									<ul style="text-align:left; ">
										<li><a ><span>총 ${ recCnt } 건</span></a></li> 
									</ul>
								</div>  
								<div >
									<ul>
										<c:set var="prevPageNo" value="${ viewPageNo - 1 }" />
										<c:set var="prevPageNo" value="${ prevPageNo < 1 ? 1 : prevPageNo }" />

										<fmt:parseNumber var="startPageNo" integerOnly="true" type="number" value="${ ( viewPageNo - 5 )/10 }" />
										<c:set var="startPageNo" value="${ startPageNo }" />
										<c:set var="startPageNo" value="${ startPageNo * 10 }" />
										<c:set var="startPageNo" value="${ startPageNo < 1 ? 1 : startPageNo }" /> 
																				
										<c:set var="endPageNo" value="${ startPageNo + 9 }" />
										<c:set var="endPageNo" value="${ endPageNo < maxPageNo ? endPageNo : maxPageNo }" />
										
										<c:set var="nextPageNo" value="${ viewPageNo + 1 }" />
										<c:set var="nextPageNo" value="${ nextPageNo < maxPageNo ? nextPageNo : maxPageNo }" /> 
										
										<li><a href="?viewPageNo=${ prevPageNo }"><span>앞 : ${ pageGrpNo } : </span></a></li>  
										<c:forEach var="pageNo" begin="${ startPageNo }" end="${ endPageNo }" varStatus="s" > 
											<fmt:formatNumber var="fmtPageNo" type="number" pattern="000" value="${ pageNo }" />
											<c:if test="${ pageNo == viewPageNo }" >
												<li><a href="?viewPageNo=${ pageNo }" style="color: yellow ; font-weight:bold; " ><span>${ fmtPageNo }</span></a></li>
											</c:if>
											<c:if test="${ pageNo != viewPageNo && pageNo <= maxPageNo }" >
												<li><a href="?viewPageNo=${ pageNo }" style="color:   blue ; font-weight:bold; " ><span>${ fmtPageNo }</span></a></li>
											</c:if> 
											<c:if test="${ pageNo != viewPageNo && pageNo > maxPageNo }" >
												<li><a                                style="color:   black ;                  " ><span>${ fmtPageNo }</span></a></li>
											</c:if>  
										</c:forEach> 
										<li><a href="?viewPageNo=${ nextPageNo }"><span>뒤</span></a></li>
									</ul>
								</div>
							</td>
						</tr>
					</tfoot>
				</table>
			</div>
		</form>
	</div>
	
	<hr/>
	
	<form method="GET">
		<ul style="padding-left: 20px;">
			<li>
				<label for="chatRoomUserName" >아이디</label>
				<input type="text" name="chatRoomUserName" value="${ param.chatRoomUserName }" />
			</li>
			<li>
				<label for="chatRoomName" >방제목</label>
				<input type="text" name="chatRoomName" value="" />
				<label> &nbsp; </label>
				<input type="submit" value ="방 만들기" />
			</li>
		</ul> 
	</form>
	
	<hr/>
	
</body>
</html>